package org.cug.controller;

import lombok.extern.slf4j.Slf4j;
import org.cug.pojo.GraduateProject;
import org.cug.pojo.Result;
import org.cug.service.GraduationProjectService;
import org.cug.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/graduateProject")
/**
 * 该控制器用于处理关于本科生毕业设计，操纵的数据库表位：graduation_project
 * state：已测验
 */
public class GraduationProjectController {
    @Autowired
    private GraduationProjectService graduationProjectService;

    @GetMapping
    public Result list() {
        log.info("查询所有本科生毕业设计信息");
        // 本地线程获取
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        List<GraduateProject> graduateProject = graduationProjectService.list(userId);
        return Result.success(graduateProject);
    }

    @PostMapping
    public Result add(@RequestBody GraduateProject graduatedesign) {
        log.info("增加本科生毕业设计信息{}", graduatedesign);
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        graduatedesign.setUserId(userId);
        graduationProjectService.add(graduatedesign);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("删除本科生毕业设计信息{}", ids);
        graduationProjectService.delete(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectid(@PathVariable Integer id) {
        log.info("查询id{}", id);
        GraduateProject graduatedesign = graduationProjectService.selectid(id);
        return Result.success(graduatedesign);
    }

    @PutMapping
    public Result alterbyid(@RequestBody GraduateProject graduatedesign) {
        log.info("修改本科生毕业设计信息{}", graduatedesign);
        graduationProjectService.alterbyid(graduatedesign);
        return Result.success();
    }
}
